const app = getApp();
var dateTimePicker = require('../../../utils/dateTimePicker.js');

function toDecimal(x) {
  var val = Number(x)
  if (!isNaN(parseFloat(val))) {
    val = val.toFixed(2);
  }
  return val;
}  

Page({
  data: {
    chose: 1,
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: new Date().getFullYear(),
    endYear: 2050,
    sChange: true,
    animationData: '',
    moreTags:'',
    moreTagsData:'',
    money:'',
    tags_pric:'',
    pre_tags_pric: '',
    tags:[],
    sendTags:[],

    checkbox:false,
    avaMoney:0,
    isSelect1:false,
    isSelect2: false,

    customItem: '全部',
    isRegion: false,

    money_type:2,

    grade_fee:'',

    age:[22,26],
    ageArr:[],
    isAge: false,

    allMoney:'',
    amount:'',
    
    allMoney2:'',
    amount2:1,
    
    finalPri:'',
    oldPri:'',
    
    tagsList:null,
    pickerData:null,
    pickerDataValue:null,
    tags2Money:'',
    pre_tags2Money: '',

    agePri:0,
    pre_agePri: 0,
    type1Pri:0,
    pre_type1Pri: 0,
    type4Pri: 0,
    pre_type4Pri: 0,
    type6Pri: 0,
    pre_type6Pri: 0,

    type1:[],
    type4:[],
    type6:[]
  },
  onLoad: function (options) {
    var that = this;
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var ageArr = [];
    for (var i = 0; i < 2; i++) {
      ageArr[i] = [];
      for (var ii = 0; ii <= 100; ii++) {
        if(ii == 0){
          ageArr[i].push('无')
        }else{
          ageArr[i].push(ii)
        }
      };
    };
    
    this.setData({
      id: options.id || '467',
      cover: options.cover || 'https://wjf.wayoxi.com/uploads/imgs/20180710/4f98e0c0f24561884dd3c19d2736d212.png',
      question_count: options.question_count,
      title: options.title,

      ageArr: ageArr,
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
    });
  },

  radioChange: function (e) {
    var that = this;
    this.setData({
      chose: e.detail.value
    });

    if(e.detail.value == '2'){
      wx.showLoading({
        title: '请稍候 ...',
      })
      wx.request({
        url: app.globalData.phpUrl + 'Publish/api',
        method: "POST",
        header: { 'content-type': 'application/json' },
        data: { api_name: 'tags', openid: app.globalData.userData.openOId},
        success: function (data) {
          var dddd = data.data.data;
          var type1 = [];
          var type2 = [];
          var type4 = [];
          var type6 = [];
          for (var bb = 0; bb < dddd.questions.length;bb++){
            if (dddd.questions[bb].type == 1){
              var ss = [];
              ss[0] = '无';
              for (var ww = 0; ww < data.data.data.questions[bb].option.length;ww++){
                ss.push(dddd.questions[bb].option[ww].content)
              }
              type1.push({ range: ss, tag_name: dddd.questions[bb].tag_name, tag_name_zh: dddd.questions[bb].tag_name_zh, 'type': dddd.grade_type, value: 0, index: bb});
            }
            if (dddd.questions[bb].type == 2) {
              var mm = [];
              for (var ww = 0; ww < data.data.data.questions[bb].option.length; ww++) {
                mm.push({ content: dddd.questions[bb].option[ww].content, checked: '', id: ww + 1})
              }
              type2.push({ range: mm, tag_name: dddd.questions[bb].tag_name, tag_name_zh: dddd.questions[bb].tag_name_zh, 'type': dddd.grade_type, 'grade_fee': 0, 'pre_grade_fee': 0, value: [], index: bb});
            }
            if (dddd.questions[bb].type == 4) {
              type4.push({ tag_name: dddd.questions[bb].tag_name, tag_name_zh: dddd.questions[bb].tag_name_zh, 'type': dddd.grade_type, region: ['全部', '全部', '全部'], tags_pric: 0, pre_tags_pric: 0, index: bb},);
            }
            if (dddd.questions[bb].type == 6) {
              type6.push({ tag_name: dddd.questions[bb].tag_name, tag_name_zh: dddd.questions[bb].tag_name_zh, 'type': dddd.grade_type, value: [0, 0], index: bb})
            }
          }
          that.setData({
            tagsData: data.data.data,
            grade_fee: data.data.data.grade_fee,
            pre_grade_fee: Number(data.data.data.pre_grade_fee),
            allMoney2: data.data.data.min_red_package,
            platform_rate: data.data.data.platform_rate,
            pre_allMoney2: Number(data.data.data.pre_min_red_package),
            type1: type1,
            type2: type2,
            type4: type4,
            type6: type6
          });

          wx.hideLoading();
          that.sum()
          that.ava()
        }
      });
    }
  },

  switchChange: function (e) {
    this.setData({
      sChange: e.detail.value
    });
  },
  
  money_type:function(){
    var that = this;
    var money_type = that.data.money_type;
    money_type == 1 ? money_type = 2 : money_type = 1;
    that.setData({
      money_type: money_type
    })
  },

  changeDateTimeColumn(e) {
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    
    e.detail.value = 1

    var bg = this.data.dateTime1;
    var ls = arr;
    if (bg[0] > ls[0]) {
      wx.showToast({
        title: '结束日期必须大于开始日期',
        icon: 'none',
        duration: 2000,
      });
    } else if (bg[0] == ls[0]) {
      if (bg[1] > ls[1]) {
        wx.showToast({
          title: '结束日期必须大于开始日期',
          icon: 'none',
          duration: 2000,
        });
      }
    } else {
      this.setData({ dateTime: e.detail.value });
    }
    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  changeDateTimeColumn1:function (e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
  changeDateTime(e) {
    var bg = this.data.dateTime1;
    var ls = e.detail.value;

    if (bg[0] > ls[0]){
      wx.showToast({
        title: '结束日期必须大于开始日期',
        icon: 'none',
        duration: 2000,
      });
    } else if (bg[0] == ls[0]){
       if(bg[1] > ls[1]){
         wx.showToast({
           title: '结束日期必须大于开始日期',
           icon: 'none',
           duration: 2000,
         });
       }
    }else{}

    this.setData({
      dateTime: e.detail.value,
      isSelect2: true
    });

  },
  changeDateTime1(e) {
    this.setData({ 
      dateTime1: e.detail.value,
      isSelect1:true
    });
  },

  //1输入红包总金额
  allMoney:function(e){
    this.setData({
      allMoney: e.detail.value
    });
    this.cutPiont()
  },
  
  //1输入样本数量
  amount: function (e) {
    this.setData({
      amount: e.detail.value == 0 ? 1 : e.detail.value
    });
    this.cutPiont()
  },

  //去小数点
  cutPiont:function(){
     var that = this;
     var allMoney = that.data.allMoney == '' ? 0 : that.data.allMoney;
     var amount = that.data.amount == '' ? 1 : that.data.amount;
     var avaMoney = toDecimal(Number(allMoney) / Number(amount));
         avaMoney = Number(toDecimal(avaMoney - (avaMoney * 0.1)));
         if (avaMoney < 0.1) {
            wx.showToast({
              title: '每个红包不得低于0.1元',
              icon:'none'
            })
         }
     that.setData({
       avaMoney: avaMoney
     })
  },

  //2地区选择
  bindRegionChange: function (e) {
    var that = this;
    var indexs = e.currentTarget.dataset.indexs;
    var selecrValue = e.detail.value;
    var region = that.data.type4;
    var type4Pri = Number(that.data.type4Pri);
    var pre_type4Pri = Number(that.data.pre_type4Pri);

    that.setData({
      ['type4[' + indexs + '].region']: e.detail.value,
      allMoney2: toDecimal(Number(that.data.allMoney2) - type4Pri),
      pre_allMoney2: toDecimal(Number(that.data.pre_allMoney2) - pre_type4Pri),
    });
    
    for (var pp = 0; pp < region.length;pp++){
      if (region[pp].region[0] == '全部') {
        region[pp].tags_pric = 0;
        region[pp].pre_tags_pric = 0
      }else{
        if (region[pp].region[1] == '全部') {
          region[pp].tags_pric = Number(toDecimal(Number(that.data.grade_fee)));
          region[pp].pre_tags_pric = Number(toDecimal(Number(that.data.pre_grade_fee)));
        }else{
          if (region[pp].region[2] == '全部') {
            region[pp].tags_pric = Number(toDecimal(Number(that.data.grade_fee) * 2));
            region[pp].pre_tags_pric = Number(toDecimal(Number(that.data.pre_grade_fee) * 2))
          }else{
            region[pp].tags_pric = Number(toDecimal(Number(that.data.grade_fee) * 3));
            region[pp].pre_tags_pric = Number(toDecimal(Number(that.data.pre_grade_fee) * 3))
          }
        }
      }
    };

    type4Pri = 0;
    pre_type4Pri = 0;

    for (var ppp = 0; ppp < region.length; ppp++){
      type4Pri = type4Pri + Number(region[ppp].tags_pric);
      pre_type4Pri = pre_type4Pri + Number(region[ppp].pre_tags_pric);
    };

    type4Pri = Number(toDecimal(type4Pri));
    pre_type4Pri = Number(toDecimal(pre_type4Pri));
  
    that.setData({
      type4: region,
      type4Pri: type4Pri,
      pre_type4Pri: pre_type4Pri,
      allMoney2: toDecimal(Number(that.data.allMoney2) + type4Pri),
      pre_allMoney2: toDecimal(Number(that.data.pre_allMoney2) + pre_type4Pri)
    });
    that.sum()
    that.ava()
  },

  //2年龄段选择
  bindAgeChange: function (e) {
    var that = this;
    var indexs = e.currentTarget.dataset.indexs;
    var selecrValue = e.detail.value;
    var type6Pri = Number(that.data.type6Pri);
    var pre_type6Pri = Number(that.data.pre_type6Pri);

    that.setData({
      ['type6[' + indexs + '].value']: e.detail.value,
      allMoney2: toDecimal(Number(that.data.allMoney2) - type6Pri),
      pre_allMoney2: toDecimal(Number(that.data.pre_allMoney2) - pre_type6Pri)
    });
    
    type6Pri = 0;
    pre_type6Pri = 0;
    
    if (that.data.type6[indexs].value[1] == 0) {
      that.setData({
        ['type6[' + indexs + '].value']: [0, 0]
      });
    }


    for (var rr = 0; rr < that.data.type6.length;rr++){
      if (that.data.type6[rr].value[0] != 0 && that.data.type6[rr].value[1] != 0){
        type6Pri = type6Pri + Number(that.data.grade_fee);
        pre_type6Pri = pre_type6Pri + Number(that.data.pre_grade_fee)
      }
    }
    
    that.setData({
      type6Pri: type6Pri,
      pre_type6Pri: pre_type6Pri,
      allMoney2: toDecimal(Number(that.data.allMoney2) + type6Pri),
      pre_allMoney2: toDecimal(Number(that.data.pre_allMoney2) + pre_type6Pri)
    });
    
    that.sum()
    that.ava()
  },

  //2年龄段联动
  multiAgeChange: function (e) {
    var that = this;
    if (e.detail.column == 0) {
      if (that.data.age[1] <= e.detail.value) {
        this.setData({
          age: [e.detail.value, e.detail.value + 1]
        })
      }
    };

    if (e.detail.column == 1) {
      if (that.data.age[1] >= e.detail.value) {
        this.setData({
          age: [e.detail.value - 1, e.detail.value]
        })
      }
    }
  },

  //2单选选择
  bindSexChange: function (e) {
    var that = this;
    var indexs = e.currentTarget.dataset.indexs;
    var type1Pri = Number(that.data.type1Pri);
    var pre_type1Pri = Number(that.data.pre_type1Pri);
    this.setData({
      ['type1[' + indexs + '].value']: e.detail.value,
      allMoney2: toDecimal(Number(that.data.allMoney2) - type1Pri),
      pre_allMoney2: toDecimal(Number(that.data.pre_allMoney2) - pre_type1Pri)
    });
    type1Pri = 0;
    pre_type1Pri = 0;
    for (var ee = 0; ee < that.data.type1.length;ee++){
      if (that.data.type1[ee].value != '' && that.data.type1[ee].value != 0){
        type1Pri = type1Pri + Number(that.data.grade_fee);
        pre_type1Pri = pre_type1Pri + Number(that.data.pre_grade_fee);
      };
    }
    this.setData({
      allMoney2: toDecimal(Number(that.data.allMoney2) + type1Pri),
      pre_allMoney2: toDecimal(Number(that.data.pre_allMoney2) + pre_type1Pri),
      type1Pri: type1Pri,
      pre_type1Pri: pre_type1Pri
    });
    that.sum()
    that.ava()
  },
  
  checkboxChange: function (e) {
    var o;
    var oo;
    var that = this;

    var checkObj = that.data.checkObj;
    var type2 = that.data.type2;
    var value = e.detail.value;
    var type2Pri = Number(type2[checkObj.indexs].grade_fee);
    var pre_type2Pri = Number(type2[checkObj.indexs].pre_grade_fee);

    value = value.sort(function (x, y) {
      return x - y;
    });

    type2[checkObj.indexs].value = [];

    for (o = 0; o < type2[checkObj.indexs].range.length; o++){
      type2[checkObj.indexs].range[o].checked = ''
    };

    that.setData({
      type2: type2,
      allMoney2: Number(toDecimal(Number(that.data.allMoney2) - type2Pri)),
      pre_allMoney2: Number(toDecimal(Number(that.data.pre_allMoney2) - pre_type2Pri))
    });


    type2Pri = 0 ;
    pre_type2Pri = 0;

    for (oo = 0; oo < value.length; oo++) {
      type2[checkObj.indexs].range[Number(value[oo]) -1].checked = true;
      type2[checkObj.indexs].value.push(type2[checkObj.indexs].range[Number(value[oo]) - 1].content);
      type2Pri = Number(toDecimal(that.data.tagsData.grade_fee * value.length));
      pre_type2Pri = Number(toDecimal(that.data.tagsData.pre_grade_fee * value.length));
    };

    
    
    if (type2[checkObj.indexs].value.length > 0){
      type2[checkObj.indexs].value = type2[checkObj.indexs].value.toString()
    };

    type2[checkObj.indexs].grade_fee = type2Pri;
    type2[checkObj.indexs].pre_grade_fee = pre_type2Pri;
    
    that.setData({
      type2: type2,
      allMoney2: Number(toDecimal(Number(that.data.allMoney2) + type2Pri)),
      pre_allMoney2: Number(toDecimal(Number(that.data.pre_allMoney2) + pre_type2Pri))
    });

    that.sum()
    that.ava()
  },

  //2弹出多选
  hideCheckbox: function (e) {
    var that = this;
    var checkObj = {};
    checkObj.indexs = e.currentTarget.dataset.indexs;
    var checkbox = that.data.checkbox;
    that.setData({
      checkbox: checkbox == false ? true : false,
      checkObj: checkObj
    })
  },
  
  //2输入红包总金额
  allMoney2: function (e) {
  },

  //2输入样本数量
  amount2: function (e) {
    var that = this;
    if (e.detail.value != ''){
      that.setData({
        amount2: e.detail.value
      })
    }else{
      that.setData({
        amount2: ''
      })
    };

    that.sum()
    that.ava()
  },

  amount2Confirm:function(e){
    var that = this;
    if (e.detail.value == '' || e.detail.value == 0) {
      that.setData({
        amount2: 1
      })
    };
    that.sum()
    that.ava()
  },
 
  //2输入红包总金额--失焦
  allMoney2Blur: function (e) {
    var that = this;
    if(Number(e.detail.value) < Number(that.data.allMoney2)){
      that.setData({
        finalPri: Number(e.detail.value),
      })
    }else{
      that.setData({
        finalPri: Number(e.detail.value),
      })
    };

    if(e.detail.value == ''){
      that.setData({
        finalPri: '',
      })
    };
    that.sum()
    that.ava()
  },
  
  //2更多人群标签
  moreTags:function(){
    var that = this;
    var tags = that.data.tags;
    for (var zz = 0; zz < that.data.type1.length; zz++) {
      if (that.data.type1[zz].value != '' && that.data.type1[zz].value != 0) {
        tags.push({ 'type': that.data.type1[zz].type, 'name': that.data.type1[zz].tag_name, 'value': that.data.type1[zz].range[that.data.type1[zz].value] });
      }
    };
    for (var zzz = 0; zzz < that.data.type4.length; zzz++) {
      if (that.data.type4[zzz].region[0] != '全部') {
        tags.push({ 'type': that.data.type4[zzz].type, 'name': 'province', 'value': that.data.type4[zzz].region[0] });
        if (that.data.type4[zzz].region[1] != '全部') {
          tags.push({ 'type': that.data.type4[zzz].type, 'name': 'city', 'value': that.data.type4[zzz].region[1] });
          if (that.data.type4[zzz].region[2] != '全部') {
            tags.push({ 'type': that.data.type4[zzz].type, 'name': 'area', 'value': that.data.type4[zzz].region[2] });
          }
        }
      }
    };
    for (var zz = 0; zz < that.data.type6.length; zz++) {
      if (that.data.type6[zz].value[0] != '' && that.data.type6[zz].value[0] != 0) {
        tags.push({ 'type': that.data.type6[zz].type, 'name': that.data.type6[zz].tag_name, 'value': that.data.ageArr[0][that.data.type6[zz].value[0]] + '~' + that.data.ageArr[1][that.data.type6[zz].value[1]] });
      }
    };

    that.setData({
      tags: tags
    });

    wx.navigateTo({
      url: '../moreTags/moreTags?openOId=' + app.globalData.userData.openOId + '&tags=' + that.data.tags 
    })

  },

  linkTo:function(){
    var that = this;
    
    //判断时间输入
    if (this.data.isSelect1 && this.data.isSelect2){ 
      var opid = app.globalData.userData.openOId;

      //时间选择
      var start_time = that.data.dateTimeArray1[0][that.data.dateTime1[0]] + '-' + that.data.dateTimeArray1[1][that.data.dateTime1[1]] + '-' + that.data.dateTimeArray1[2][that.data.dateTime1[2]] + ' ' + that.data.dateTimeArray1[3][that.data.dateTime1[3]] + ':' + that.data.dateTimeArray1[4][that.data.dateTime1[4]];

      var end_time = that.data.dateTimeArray[0][that.data.dateTime[0]] + '-' + that.data.dateTimeArray[1][that.data.dateTime[1]] + '-' + that.data.dateTimeArray[2][that.data.dateTime[2]] + ' ' + that.data.dateTimeArray[3][that.data.dateTime[3]] + ':' + that.data.dateTimeArray[4][that.data.dateTime[4]];
 
      if (that.data.chose == '1'){
        if (that.data.sChange == false){
          var daTa = {
            api_name: 'release',
            openid: opid,
            questionnaire_id: that.data.id,
            start_time: start_time,
            end_time: end_time,
            'type': that.data.chose,
            is_red_package: 2,
            money_type: '',
            tags: '',
            pay_money: '',
            count: ''
          }
          that.push(daTa)
        } else if (that.data.sChange == true){
          if (that.data.allMoney == '' || that.data.amount == ''){
            wx.showToast({
              title: '请输入红包金额和样本数量!',
              icon: 'none',
              duration: 2000,
            });
           }else{
            if (that.data.amount > 5000){
              wx.showToast({
                title: '样本数量不得大于5000!',
                icon: 'none',
                duration: 2000,
              });
            } else if (that.data.amount == 0) {
              wx.showToast({
                title: '样本数量不能为0!',
                icon: 'none',
                duration: 2000,
              });
            } else if (that.data.avaMoney < 0.1) {
              wx.showToast({
                title: '每个红包金额不得低于0.1元!',
                icon: 'none',
                duration: 2000,
              });
            }else{
              ///////请求1 有红包
              var daTa = {
                api_name: 'release',
                openid: opid,
                questionnaire_id: that.data.id,
                start_time: start_time,
                end_time: end_time,
                'type': that.data.chose,
                is_red_package: 1,
                money_type: that.data.money_type,
                tags: '',
                pay_money: that.data.allMoney,
                count: that.data.amount,
              }
              that.push(daTa)
            }
           }
        }
      } else if (that.data.chose == '2'){
        if (that.data.amount2 == '' || that.data.finalPri == ''){
          wx.showToast({
            title: '请输入红包金额和样本数量!',
            icon: 'none',
            duration: 2000,
          });
        } else if (that.data.amount2 > 5000){
          wx.showToast({
            title: '样本数量不得大于5000!',
            icon: 'none',
            duration: 2000,
          });
        } else if (that.data.finalPri < that.data.allMoney2 * that.data.amount2){
          wx.showToast({
            title: '金额不得低于' + that.data.tipsMoney +'!',
            icon: 'none',
            duration: 2000,
          });
        }else{
          var tags = that.data.tags;
          var sendTags = that.data.sendTags;
          sendTags = sendTags.concat(tags);

          for (var zz = 0; zz < that.data.type1.length;zz++){
            if (that.data.type1[zz].value != '' && that.data.type1[zz].value != 0){
              sendTags.push({ 'type': that.data.type1[zz].type, 'name': that.data.type1[zz].tag_name[0], 'value': that.data.type1[zz].range[that.data.type1[zz].value], select_type: 1});
            }
          }

          for (var zz = 0; zz < that.data.type2.length; zz++) {
            if (that.data.type2[zz].value != '' && that.data.type2[zz].value != 0) {
              sendTags.push({ 'type': that.data.type2[zz].type, 'name': that.data.type2[zz].tag_name[0], 'value': that.data.type2[zz].value, select_type: 2 });
            }
          }

          for (var zzz = 0; zzz < that.data.type4.length; zzz++) {
            if (that.data.type4[zzz].region[0] != '全部') {
              sendTags.push({ 'type': that.data.type4[zzz].type, 'name': that.data.type4[zzz].tag_name[0], 'value': that.data.type4[zzz].region[0], select_type:4});
              if (that.data.type4[zzz].region[1] != '全部') {
                sendTags.push({ 'type': that.data.type4[zzz].type, 'name': that.data.type4[zzz].tag_name[1], 'value': that.data.type4[zzz].region[1], select_type: 4});
                if (that.data.type4[zzz].region[2] != '全部') {
                  sendTags.push({ 'type': that.data.type4[zzz].type, 'name': that.data.type4[zzz].tag_name[2], 'value': that.data.type4[zzz].region[2], select_type: 4});
                }
              }
            }
          }

          for (var zz = 0; zz < that.data.type6.length; zz++) {
            if (that.data.type6[zz].value[0] != '' && that.data.type6[zz].value[0] != 0) {
              sendTags.push({ 'type': that.data.type6[zz].type, 'name': that.data.type6[zz].tag_name[0], 'value': that.data.ageArr[0][that.data.type6[zz].value[0]] + '~' + that.data.ageArr[1][that.data.type6[zz].value[1]], select_type: 6});
            }
          }

          var daTa = {
            api_name: 'release',
            openid: opid,
            questionnaire_id: that.data.id,
            start_time: start_time,
            end_time: end_time,
            'type': that.data.chose,
            is_red_package: '1',
            money_type: '',
            tags: sendTags,
            pay_money: that.data.finalPri,
            count: that.data.amount2
          };
          that.setData({
            sendTags:[]
          })
          that.push(daTa)
        }
      }

    }else{
      wx.showToast({
        title: '请输入开始结束时间',
        icon: 'none',
        duration: 2000,
      });
    }

  },

  onHide:function(){
    var that = this;
  },

  push: function(data){
    var that = this;
    wx.showLoading({
      title: '提交中...',
    })
    wx.request({
      url: app.globalData.phpUrl + 'Publish/api',
      method: "POST",
      header: { 'content-type': 'application/json' },
      data: data,
      success: function (data) {
        console.log('问卷发布成功!')
        if(data.data.code == 2){
          var timeStamp = data.data.data.timeStamp;
          var packages = data.data.data.package;
          var paySign = data.data.data.paySign;
          var nonceStr = data.data.data.nonceStr;
          var signType = data.data.data.signType;
          var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": signType, "nonceStr": nonceStr };
          that.pay(param)
        } else if(data.data.code == -1){
          wx.showToast({
            title: data.data.msg,
            icon: 'none',
            duration: 2000
          });
          return
        }else{
          wx.hideLoading();
          wx.redirectTo({
            url: '../reviewItem/reviewItem?id=' + that.data.id + "&title=" + that.data.title + "&question_count=" + that.data.question_count + "&cover=" + that.data.cover,
          });
        }
      }
    });
  },

  pay: function (param) {
    var that = this;
    wx.requestPayment({
      timeStamp: param.timeStamp,
      nonceStr: param.nonceStr,
      package: param.package,
      signType: param.signType,
      paySign: param.paySign,
      success: function (res) {
        wx.hideLoading();
        wx.redirectTo({
          url: '../reviewItem/reviewItem?id=' + that.data.id + "&title=" + that.data.title + "&question_count=" + that.data.question_count + "&cover=" + that.data.cover,
        });
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '取消支付',
          icon: 'none',
          duration: 2000
        });

      },
      complete: function () {
        console.log('支付成功!')
      }
    })
  },
  
  sum:function(){
    var that = this;
    var allMoney2 = that.data.allMoney2;
    var amount2 = that.data.amount2;
        that.setData({
          tipsMoney: Number(toDecimal(amount2 * allMoney2))
        })
  },

  ava:function(){
    var that = this;
    var amount2 = that.data.amount2;
    var finalPri = that.data.finalPri;
    var allMoney2 = that.data.allMoney2;
    
    that.setData({
      noConfirmAva: Number(toDecimal(Number(that.data.pre_allMoney2 / amount2)))
    });

    that.setData({
      confirmAva: Number(toDecimal(Number((finalPri - that.data.allMoney2) * that.data.platform_rate + that.data.noConfirmAva) / amount2))
    });
  }
})
